<template>
  <el-form v-if="!userloading" ref="postForm" :model="postForm" :rules="rules" label-position="right" label-width="100px">
    <el-form-item label="用户名">
      {{ postForm.username }}
    </el-form-item>
    <el-form-item prop="oldPassword" label="原密码">
      <el-input v-model.trim="postForm.oldPassword" type="password" style="width:300px" />
    </el-form-item>
    <el-form-item prop="newPassword" label="新密码">
      <el-input v-model.trim="postForm.newPassword" type="password" style="width:300px" />
    </el-form-item>
    <el-form-item prop="newPassword2" label="重复密码">
      <el-input v-model.trim="postForm.newPassword2" type="password" style="width:300px" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" :loading="loading" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        oldPassword: [{ validator: validateRequire }],
        newPassword: [
          { validator: validateRequire },
          { min: 6, message: '长度不能小于6个字符', trigger: 'blur' }
        ],
        newPassword2: [{ validator: validateRequire }]
      },
      userloading: false,
      loading: false,
      myHeaders: {},
      postForm: {
        oldPassword: '',
        newPassword: '',
        newPassword2: ''
      }
    }
  },
  created() {
    this.getUser()
  },
  mounted() {
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
  },
  methods: {
    getUser() {
      this.postForm.username = JSON.parse(localStorage.getItem('user')).username
      // this.userloading = true
      // this.$http
      //   .get('/api/sysUser/getPersonalInfo')
      //   .then(response => {
      //     this.userloading = false
      //     if (response.data.success === true) {
      //       this.postForm.username = response.data.result.username
      //     } else {
      //       console.log('获取失败:' + response.data.message)
      //       this.$message({
      //         message: '获取失败:' + response.data.message,
      //         type: 'error'
      //       })
      //     }
      //   })
      //   .catch(function(error) {
      //     console.log(error)
      //     this.userloading = false
      //   })
    },
    submit() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          if (this.postForm.newPassword !== this.postForm.newPassword2) {
            this.$message({
              message: '两次密码输入不一致',
              type: 'error'
            })
          } else {
            const para = {
              oldPassword: this.postForm.oldPassword,
              newPassword: this.postForm.newPassword
            }
            this.loading = true
            this.$http
              .post('/api/user/updatePassword', para)
              .then(response => {
                this.loading = false
                if (response.data.success === true) {
                  this.$message({
                    message: '保存成功',
                    type: 'success'
                  })
                } else {
                  this.$message({
                    message: '保存失败:' + response.data.message,
                    type: 'error'
                  })
                }
              })
              .catch(() => {
                this.loading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
